﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports Newtonsoft.Json
Imports System
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Text.RegularExpressions

Namespace roncliProductions.LibWowArmory.Calendar.Month.World

    ''' <summary>
    ''' A class that defines the events for a given month on a player's calendar
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CalendarMonthWorld
        Inherits WowArmoryData

        Private cmwCalendar As New Schema.calendarMonthWorld
        Private eEvents As New Collection(Of CalendarEvent)
        Private dtMonth As Date

        ''' <summary>
        ''' The realm of the character for the calendar to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm As String

        ''' <summary>
        ''' The name of the character for the calendar to lookup
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Name As String

        ''' <summary>
        ''' The month of the character's calendar to lookup.  Note that only month and year are stored.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Month As Date
            Get
                Return dtMonth
            End Get
            Set(ByVal value As Date)
                dtMonth = New Date(value.Year, value.Month, 1)
            End Set
        End Property

        ''' <summary>
        ''' The type of calendar events to retrieve.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property EventType As Enums.CalendarType

        ''' <summary>
        ''' The URL that will be used to retrieve the character's calendar from the armory
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Dim strType As String = ""
                Select Case EventType
                    Case Enums.CalendarType.BattlegroundHolidays
                        strType = "bg"
                    Case Enums.CalendarType.DarkmoonFaire
                        strType = "darkmoon"
                    Case Enums.CalendarType.RaidLockouts
                        strType = "raidLockout"
                    Case Enums.CalendarType.RaidResets
                        strType = "raidReset"
                    Case Enums.CalendarType.WeeklyHolidays
                        strType = "holidayWeekly"
                    Case Enums.CalendarType.WorldHolidays
                        strType = "holiday"
                End Select
                Return String.Format(CultureInfo.InvariantCulture, "http://www.wowarmory.com/vault/calendar/month-world.json?callback=calendar.loadEvents&r={0}&n={1}&year={2}&month={3}&loc=en_us&type={4}", Realm, Name, dtMonth.Year, dtMonth.Month, strType)
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request requires login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return True
            End Get
        End Property

        ''' <summary>
        ''' The month's calendar type
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CalendarType As String
            Get
                Return cmwCalendar.calendarType
            End Get
        End Property

        ''' <summary>
        ''' The timezone offset of the event's server
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property TimezoneOffset As TimeSpan
            Get
                Return New TimeSpan(0, 0, 0, 0, CInt(cmwCalendar.tz))
            End Get
        End Property

        ''' <summary>
        ''' The list of events for the month
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Events As Collection(Of CalendarEvent)
            Get
                Return eEvents
            End Get
        End Property

        ''' <summary>
        ''' A constructor to create a new calendar month for a character
        ''' </summary>
        ''' <param name="strUsername">The World of Warcraft/battle.net account username</param>
        ''' <param name="strPassword">The World of Warcraft/battle.net account password</param>
        ''' <param name="strRealm">The realm of the character for the calendar to lookup</param>
        ''' <param name="strName">The name of the character for the calendar to lookup</param>
        ''' <param name="dtMonth">The month of the character's calendar to lookup.  Note that only month and year are stored.</param>
        ''' <param name="ctEventType">The type of calendar events to return</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strUsername As String, ByVal strPassword As String, ByVal strRealm As String, ByVal strName As String, ByVal dtMonth As Date, ByVal ctEventType As Enums.CalendarType)
            Username = strUsername
            Password = strPassword
            Realm = strRealm
            Name = strName
            Month = dtMonth
            EventType = ctEventType
            Load()
        End Sub

        ''' <summary>
        ''' Loads and deserialzes the JSON data
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            MyBase.Retrieve()

            Data = Regex.Match(Data, "calendar.loadEvents\((.*)\);").Groups(1).Value

            cmwCalendar = CType(JsonConvert.DeserializeObject(Data, GetType(Schema.calendarMonthWorld)), Schema.calendarMonthWorld)

            eEvents.Clear()

            If cmwCalendar Is Nothing Then
                Throw New InvalidOperationException("The calendar did not return any information.")
                Exit Sub
            End If

            If cmwCalendar.events Is Nothing Then
                Exit Sub
            End If

            For Each eEvent As Schema.calendarEvent In cmwCalendar.events
                eEvents.Add(New CalendarEvent(eEvent.calendarType, eEvent.calendarTypeId, eEvent.description, eEvent.end, eEvent.icon, eEvent.priority, eEvent.start, eEvent.summary, Me))
            Next
        End Sub

    End Class

End Namespace